package com.xinqi.common.third.oss.avatar;

import com.xinqi.common.core.utils.DateUtils;
import com.xinqi.common.third.oss.util.AliYunOssUtil;
import com.xinqi.common.third.util.FileUtils;

import java.io.InputStream;
import java.util.Base64;

/**
 * 阿里云生成OSS头像
 *
 * @author dzy
 */
public class AvatarOssUtil {

    private static String baseUrl = "https://oss.xinqiclass.com/avatar/base/avatar_bg.jpg?x-oss-process=image/watermark,type_d3F5LW1pY3JvaGVp,size_70,text_,color_FFFFFF,g_center";

    public static String creatAvatar(String text) {

        // 文字转Base64Url
        String encodedData = Base64.getUrlEncoder().withoutPadding().encodeToString(text.getBytes());
        // 动他生成OSS头像
        String avatarUrl = baseUrl.replace("text_", "text_" + encodedData);
        // 生成输入流
        InputStream inputStream = FileUtils.urlToStream(avatarUrl);
        // 上传阿里云生地址
        String targetPath = "avatar/" + DateUtils.getDate();
        return AliYunOssUtil.uploadFileStream(inputStream, targetPath, "png");

    }

}
